#!/usr/bin/env bash
# Summary: Show the current Node version and its origin
#
# Shows the currently selected Node version and how it was
# selected. To obtain only the version string, use `nodenv
# version-name'.

set -e
[ -n "$NODENV_DEBUG" ] && set -x

VERSION_NAME="$(nodenv-version-name)"
VERSION_PATH="$(nodenv-prefix "$VERSION_NAME" 2>/dev/null || true)"

while [ -L "$VERSION_PATH" ]; do
  READLINK=$(type -p greadlink readlink | head -1)
  [ -n "$READLINK" ] || break

  VERSION_PATH=$($READLINK "$VERSION_PATH")
  ALIAS=$(basename "$VERSION_PATH")
done

echo "$VERSION_NAME ${ALIAS+=> $ALIAS }(set by $(nodenv-version-origin))"
